#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QActionGroup>
#include <QSpinBox>
#include <QFontComboBox>
#include <QLabel>
#include <QProgressBar>
#include <QMessageBox>
#include <QDebug>
#include <QInputDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QActionGroup *actiongroup = new QActionGroup(this);
    actiongroup->addAction(ui->chinese_action);
    actiongroup->addAction(ui->english_action);
    actiongroup->setExclusive(true);

    QSpinBox *spinBox = new QSpinBox(this);
    spinBox->setMaximum(50);
    spinBox->setMinimum(5);
    spinBox->setValue(ui->textEdit->font().pointSize());
    spinBox->setMinimumWidth(50);
    ui->toolBar->addWidget(spinBox);

    QFontComboBox *qFontComboBox = new QFontComboBox(this);
    qFontComboBox->setMinimumWidth(150);
    qFontComboBox->setFont(ui->textEdit->font());
    ui->toolBar->addWidget(qFontComboBox);

    ui->toolBar->addSeparator();
    ui->toolBar->addAction(ui->actClose);

    QLabel *labFile = new QLabel(this);
    labFile->setMinimumWidth(150);
    labFile->setText("文件名：");
    ui->statusbar->addWidget(labFile);
    QProgressBar *qProgressBar = new QProgressBar(this);
    qProgressBar->setMinimum(5);
    qProgressBar->setMaximum(50);
    qProgressBar->setValue(ui->textEdit->font().pointSize());

    ui->statusbar->addWidget(qProgressBar);
    QLabel *labelInfo = new QLabel("permanent");
    ui->statusbar->addPermanentWidget(labelInfo);

    connect(spinBox, &QSpinBox::valueChanged,this, &MainWindow::spinBox_changed);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::spinBox_changed(int size)
{
    QTextCharFormat format = ui->textEdit->currentCharFormat();
    format.setFontPointSize(size);
    ui->textEdit->setCurrentCharFormat(format);

}



void MainWindow::on_file_new_triggered()
{
    QMessageBox::StandardButton d = QMessageBox::information(this, "info","info text");
    Q_UNUSED(d);
}


void MainWindow::on_file_open_triggered()
{
    QMessageBox::StandardButton d = QMessageBox::critical(this, "error","error text");
    Q_UNUSED(d);
}


void MainWindow::on_file_save_triggered()
{
    QString dlgTitle = "输入文字对话框";
    QString txtLabel = "请输入文件名";
    QString iniInput = "新建文件.txt";
    QLineEdit::EchoMode echoMode = QLineEdit::Password;
    bool ok = false;
    QString text = QInputDialog::getText(this, dlgTitle, txtLabel, echoMode, iniInput, &ok);
    if(ok && !text.isEmpty()) {
        ui->textEdit->append(text);
    }
}

